package com.sun.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class MybatisConfig {
    //定义bean，SqlSessionFactoryBean，用于产生SqlSessionFactory对象
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        //设置数据源
        ssfb.setDataSource(dataSource);
        //设置日志
        return ssfb;
    }
    //定义bean，返回MapperScannerConfigurer对象
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        //MapperScannerConfigurer对象也是MyBatis提供的专用于整合的jar包中的类，
        //用来处理原始配置文件中的mappers相关配置，加载数据层的Mapper接口类
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //核心属性basePackage，就是用来设置所扫描的包路径
        msc.setBasePackage("com.sun.mapper");
        return msc;
    }
}
